package com.zmc.lostfound.auth.controller;

import com.zmc.lostfound.auth.service.UserRoleService;
import com.zmc.lostfound.auth.vo.UserRoleSaveVO;
import com.zmc.lostfound.common.utils.Result;
import com.zmc.lostfound.common.valid.AddGroup;
import com.zmc.lostfound.common.valid.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZMC
 * @description 用户&角色关联信息管理接口
 * @email 2869581855@qq.com
 * @date 2023-07-05 15:09:50
 */
@RestController
@RequestMapping("/userRole")
@Api(value = "用户&角色关联信息管理接口", tags = "用户&角色关联信息管理接口")
public class UserRoleController {

    private final UserRoleService userRoleService;

    public UserRoleController(UserRoleService userRoleService) {
        this.userRoleService = userRoleService;
    }

    @ApiOperation("新增用户&角色关联信息")
    @PostMapping("/insertUserRole")
    public Result insertUserRole(@RequestBody @Validated(value = {AddGroup.class}) UserRoleSaveVO userRoleSaveVO) {
        return userRoleService.insertUserRole(userRoleSaveVO);
    }

    @ApiOperation("修改用户&角色关联信息")
    @PutMapping("/updateUserRole")
    public Result updateUserRole(@RequestBody @Validated(value = {UpdateGroup.class}) UserRoleSaveVO userRoleSaveVO) {
        return userRoleService.updateUserRole(userRoleSaveVO);
    }

    @ApiOperation("按主键ids批量删除用户&角色关联信息")
    @DeleteMapping("/deleteBatchByIds")
    public Result deleteBatchByIds(@RequestBody List<Long> ids) {
        return userRoleService.deleteBatchByIds(ids);
    }

    @ApiOperation("按用户id批量删除用户&角色关联信息")
    @DeleteMapping("/deleteBatchByUserId/{userId}")
    public Result deleteBatchByUserId(@PathVariable("userId") Long userId) {
        return userRoleService.deleteByUserIdAndRoleId(userId, null);
    }

    @ApiOperation("按角色id批量删除用户&角色关联信息")
    @DeleteMapping("/deleteBatchByRoleId/{roleId}")
    public Result deleteBatchByRoleId(@PathVariable("roleId") Long roleId) {
        return userRoleService.deleteByUserIdAndRoleId(null, roleId);
    }

    @ApiOperation("按用户id和角色id删除用户&角色关联信息")
    @DeleteMapping("/delete/{userId}/and/{roleId}")
    public Result deleteByUserIdAndRoleId(@PathVariable("userId") Long userId, @PathVariable("roleId") Long roleId) {
        return userRoleService.deleteByUserIdAndRoleId(userId, roleId);
    }

}
